package com.photo.tagger;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MenuActivity extends Activity implements OnClickListener
{

	private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 1;
	private static final int OPEN_GALLERY_BROWSER_ACTIVITY_REQUEST_CODE = 2;
	private Uri fileUri;
	File file;

	/*
	 * @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO
	 * Auto-generated method stub return super.onCreateOptionsMenu(menu); }
	 */
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.select_pic);
		createInstances();
	}

	private void createInstances()
	{
		Button captureButton = (Button) findViewById(R.id.capture_button);
		Button chooseButton = (Button) findViewById(R.id.chooseButton);

		captureButton.setOnClickListener(this);
		chooseButton.setOnClickListener(this);

	}
	public void onClick(View b)
	{
		switch (b.getId())
		{
		case R.id.chooseButton:
			openGallery();
			break;
		case R.id.capture_button:
			captureImage();
			break;

		}
	}
	public void captureImage()
	{
		

		// file = new File(Environment.getExternalStorageDirectory()+
		// "/images/make_machine_example.jpg");
		// fileUri = Uri.fromFile(file);

		Intent intent = new Intent(
				android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		File mediaStorageDir = new File(
				Environment
						.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
				"MyCameraApp");
		String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
				.format(new Date());
		File mediaFile;
		mediaFile = new File(mediaStorageDir.getPath() + File.separator
				+ "IMG_" + timeStamp + ".jpg");
		intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
		// create a file to save the image
		ContentValues values = new ContentValues();

		intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the
		// image file name
		fileUri = getContentResolver().insert(
				MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

		// to save the image
		intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the
		// image
		// file name

		// start the image capture Intent
		startActivityForResult(intent,
				CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
	}



	public void openGallery()
	{
		// To open up a gallery browser
		Intent intent = new Intent();
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
		startActivityForResult(Intent.createChooser(intent, "Select Picture"),
				OPEN_GALLERY_BROWSER_ACTIVITY_REQUEST_CODE);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{

		//Log.i("tag", "resulgfgdft");
		if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE)
		{
			if (resultCode == RESULT_OK)
			{
				 showResult();
			}
		}
		if (requestCode == OPEN_GALLERY_BROWSER_ACTIVITY_REQUEST_CODE)
		{
			if (resultCode == RESULT_OK)
			{
				fileUri = data.getData();
				showResult();
				// Image captured and saved to fileUri specified in the Intent

			}
		}

	}
	public String getRealPathFromURI(Uri contentUri)
	{
		// can post image
		String[] proj = { MediaStore.Images.Media.DATA };
		Cursor cursor = managedQuery(contentUri, proj, // Which columns to
				// return
				null, // WHERE clause; which rows to return (all rows)
				null, // WHERE clause selection arguments (none)
				null); // Order-by clause (ascending by name)
		int column_index = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}
	Bitmap bitmap;
	public void showResult()
	{
		file = new File(getRealPathFromURI(fileUri));
		if (file.exists())
		{
			Log.i("tag", "salu");
			String path=file.getAbsolutePath();
			Log.i("tag", "hammmmaaaa path vachindhi");
			Log.i("tag", "thu.. decode ayyi sasthale..");
			bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.chata);
			Log.i("tag", "thu.. decode ayyi sasthale..");
			bitmap=BitmapFactory.decodeFile(path);
			Log.i("tag", "decode kuda aindhi rooo.");
			ImageView myImage = (ImageView) findViewById(R.id.backImage);
			myImage.setImageBitmap(bitmap);
			Log.i("tag", "result pothundhi");
			sendResult();

		}
	}

	

	private void sendResult()
	{
		//Intent intent = new Intent(this, com.photo.tagger.TagUI.class);
		//intent.putExtra("bmp", myBitmap);
		//ByteArrayOutputStream bs = new ByteArrayOutputStream();
		//bitmap.compress(Bitmap.CompressFormat.JPEG, 50, bs);
		//intent.putExtra("byteArray", bs.toByteArray());
		//Log.i("tag", "first lo last line");
		//startActivity(intent);
		
		
		Intent intent = new Intent(this, TagUI1.class);
		Log.i("tag", "intent");
		Bundle b=new Bundle();
		b.putString("imageUri", fileUri.toString());
		Log.i("tag", "string ayyindi");
		intent.putExtras(b);
		Log.i("tag", "sdfaasdf");
		startActivity(intent);
		Log.i("tag", "ekkade undi");


		//Log.i("tag", "call");
		//startActivity(intent);
		//Log.i("tag", "call");

	}
}
